<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>餐饮总部订单管理</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        .layui-form-label{
            width: 120px;
        }
        @media screen and (max-width: 450px){
            .layui-form-item .layui-input-inline {
                margin-left: 150px;
            }
        }
        .canteenTable_title{
            margin-top: 10px;
            font-weight: bold;
        }
        
        #tableForm{
            padding: 10px 20px;
            display: none;

        }
        .layui-form-select dl dd.layui-this{
            background-color: #ffffff;
            color: #333333;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">订单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="shopOrderNo" autocomplete="off" class="layui-input" placeholder="订单编号" maxlength="30">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">供应商名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="supplierName" autocomplete="off" class="layui-input" placeholder="供应商名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">餐饮总部名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="foodManagerName" autocomplete="off" class="layui-input" placeholder="餐饮总部名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">食堂名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="canteenName" autocomplete="off" class="layui-input" placeholder="食堂名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="productName" autocomplete="off" class="layui-input" placeholder="商品名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">订单状态</label>
                            <div class="layui-input-inline">
                                <div id="orderStatus"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否改单</label>
                            <div class="layui-input-inline">
                                <select name="updateOrderStatus" lay-filter="updateOrderStatus">
                                    <option value=""></option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否生成账单</label>
                            <div class="layui-input-inline">
                                <select name="foodManagerOrderBillStatus" lay-filter="foodManagerOrderBillStatus">
                                    <option value=""></option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">下单时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="selectTime" placeholder=" - ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary reReset" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>

        <div id="tableForm">
            <div class="canteenTable_title">要生成账单的订单列表</div>
            <table class="layui-hide" id="canteenTable" lay-filter="canteenTable"></table>
        </div>
    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-sm" lay-event="change">生成账单</button>
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="export">导出</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="plugins/layui/modules/xm-select.js"></script>
<script>
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'form', 'laydate'], function() {
        var http = layui.http,
            table = layui.table,
            form = layui.form,
            laydate = layui.laydate;
    
        var excelList,currPage,canteenTable
        var canteenIdSet = [] // 订单id集合
        var canteenIdSetList = [] // 选中订单
		
		$('.reReset').click();

        // 重置
        window.onReset = function() {
            orderStatus.setValue([])
        }

        laydate.render({
            elem: '#selectTime'
            ,theme: '#fbaf7e'
            ,range: "-"
        });

        var orderStatus = xmSelect.render({
            el: '#orderStatus',
            name: 'orderStatusSet',
            style: {
                borderColor: '#e6e6e6'
            },
            theme: {
                color: '#f56643 ',
            }, 
            data: [
                {name: '待接单', value: 4},
                {name: '待发货', value: 5},
                {name: '已发货', value: 8},
                {name: '已完成', value: 9},
                {name: '已取消', value: 10},
                {name: '待付款', value: 3},
            ]
        })

        var selectTable;
        selectTable = http.table({
            elem: '#selectTable',
            url: 'admin/shop/order/listPage',
            method: 'get',
            id: 'selectTable',
            toolbar: '#toolbarDemo',
            where: {
                queryType: 2
            },
            done: function(res, curr, count){
			    excelList = res.data;
                currPage = curr
                var isAll = []

                res.data.forEach(function(value, key) {
                    //这里必须选择点击方法，不知为何添加layui-form-checked class属性会造成二次点击（即下次点击必须点击两下才能生效）bug，
                    //所有类似的手动修改class貌似都会造成二次效果
                    // // console.log(canteenIdSet)
                    if(canteenIdSet.indexOf(value.shopOrderNo)!==-1){
                        value["LAY_CHECKED"]='true';
                        $(".layui-table tr[data-index="+key+"] input[type='checkbox']").prop('checked',true);
                        $(".layui-table tr[data-index="+key+"] input[type='checkbox']").next().addClass('layui-form-checked')
                    }
                    if(value.foodManagerOrderBillStatus==0&&value.orderStatus==9&&value.billType==2){
                        isAll.push(value)
                    }else{
                        $(".layui-table tr[data-index="+key+"] input[type='checkbox']").prop('disabled',true);
                        $(".layui-table tr[data-index="+key+"] input[type='checkbox']").next().addClass('layui-btn-disabled')
                    }
                })
                var checkStatus = table.checkStatus('selectTable');
                if(checkStatus.data.length != 0 && checkStatus.data.length == isAll.length){ // 是否全选
                    $(' .layui-table-header th[data-field="0"] input[type="checkbox"]').prop('checked', true);
                    $('.layui-table-header th[data-field="0"] input[type="checkbox"]').next().addClass('layui-form-checked');
                }
                // $(".layui-table-header th[data-field='0'] .layui-form-checkbox").hide()
			},
            cols: [[
                {type:'checkbox',fixed: 'left'}
                ,{field:'shopOrderNo', title: '订单编号', align : 'center', width: 235,
                    templet: function(d){
                        var html = ''
                        html += '<a class="buttoncolor" href="orderDetail.html?id='+d.shopOrderNo+'">'+d.shopOrderNo+'</a>'
                        return html
                    }
                }
                ,{field:'supplierName', title: '供应商名称', align : 'center'}
                ,{field:'foodManagerName', title: '餐饮总部名称', align : 'center'}
                ,{field:'canteenName', title: '食堂名称', align : 'center'}
                ,{field:'orderTotalMoney', title: '金额', align : 'center'}
                ,{field:'billType', title: '结账模式', align : 'center',
					templet: function (d) {
						return d.billType==1?'立结':'记账';
                    }
                }
                ,{field:'filterUpdateOrderStatus', title: '状态', align : 'center',
					templet: function (d) {
                        var html = ''
                        switch(d.filterUpdateOrderStatus){
                            // case 1:
                            //     html = '<span style="color: #1ABC9C">待提交</span>'
                            //     break
                            case 3:
                                html = '<span style="color: #FF0000">待付款</span>'
                                break
                            case 4:
                                html = '<span style="color: #1ABC9C">待接单</span>'
                                break
                            case 5:
                                html = '<span style="color: #0066FF">待发货</span>'
                                break
                            case 8:
                                html = '<span style="color: #000000">已发货</span>'
                                break
                            case 9:
                                html = '<span style="color: #FF9900">已完成</span>'
                                break
                            case 10:
                                html = '<span style="color: #FF0000">已取消</span>'
                                break
                        }
                        return html
                    }
                }
                ,{field:'updateStatus', title: '是否改单', align : 'center',
					templet: function (d) {
						return d.updateStatus==1?'是':'否';
                    }
                }
                // ,{field:'productName', title: '商品名称', align : 'center'}
                ,{field:'goodCount', title: '商品数量', align : 'center'}
				,{field:'createTime', title: '下单时间', align : 'center',width: 170}
                ,{field:'foodManagerOrderBillStatus', title: '是否生成账单', align : 'center',
					templet: function (d) {
						return d.foodManagerOrderBillStatus==1?'是':'否';
                    }
                }
            ]]
        });
        
        table.on('checkbox(selectTable)', function(obj){
            if(obj.type == 'all') { // 全选
                if(obj.checked){
                    excelList.forEach(function(value, key) {
                        if(value.supplierOrderBillStatus==0&&value.filterUpdateOrderStatus==9){ // 可选
                            canteenIdSetList.push(value)
                            canteenIdSet.push(value.shopOrderNo);
                        }else{
                            $(".layui-table tr[data-index="+key+"] input[type='checkbox']").prop('checked',false);
                            $(".layui-table tr[data-index="+key+"] input[type='checkbox']").next().removeClass('layui-form-checked')
                        }
                    })
                }else{
                    for(var i=0;i<canteenIdSet.length;i++){
                        for(var j=0;j<excelList.length;j++){
                            if(canteenIdSet[i]==excelList[j].shopOrderNo){
                                canteenIdSetList.splice(i,1)
                                canteenIdSet.splice(i,1);
                            }
                        }
                    }
                }
            }else{
                if(obj.checked==true){
                    if(obj.type=='one'){
                        canteenIdSetList.push(obj.data)
                        canteenIdSet.push(obj.data.shopOrderNo);
                    }else{
                        for(var i=0;i<excelList.length;i++){
                            canteenIdSetList.push(excelList[i])
                            canteenIdSet.push(excelList[i].shopOrderNo);
                        }
                    }
                }else{
                    if(obj.type=='one'){
                        for(var i=0;i<canteenIdSet.length;i++){
                            if(canteenIdSet[i]==obj.data.shopOrderNo){
                                canteenIdSetList.splice(i,1)
                                canteenIdSet.splice(i,1);
                            }
                        }
                    }else{
                        for(var i=0;i<canteenIdSet.length;i++){
                            for(var j=0;j<excelList.length;j++){
                                if(canteenIdSet[i]==excelList[j].shopOrderNo){
                                    canteenIdSetList.splice(i,1)
                                    canteenIdSet.splice(i,1);
                                }
                            }
                        }
                    }
                }
            }
            // // console.log(canteenIdSetList)
            

        });
        // 回显选中的订单
        function canteenFun(){
            canteenTable = http.table1({
                elem: '#canteenTable',
                id: 'canteenTable',
                cols: [[
                    {title:'序号',type:'numbers'},
                    {field:'shopOrderNo', title: '订单编号', align : 'center', width: 185,
                        templet: function(d){
                            var html = ''
                            html += '<a class="buttoncolor" href="orderDetail.html?id='+d.shopOrderNo+'">'+d.shopOrderNo+'</a>'
                            return html
                        }
                    }
                    ,{field:'supplierName', title: '供应商名称', align : 'center'}
                    ,{field:'foodManagerName', title: '餐饮总部名称', align : 'center'}
                    ,{field:'orderTotalMoney', title: '金额', align : 'center'}
                    ,{field:'createTime', title: '下单时间', align : 'center',width: 170}
                    ,{field:'', title: '操作', align: 'center',width: 130,
                        templet: function (d) {
                            var html = '';
                            html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" >删除</button>'
                            return html;
                        }
                    }
                ]],
                limit: canteenIdSetList.length,
                data: canteenIdSetList
            })
            // table操作
            table.on('tool(canteenTable)', function(obj){
                if(obj.event=='del'){
                    // 删除
                    var index = $(obj.tr).eq(0).data('index')
                    canteenIdSetList.splice(index,1)
                    canteenIdSet.splice(index,1)
                    canteenFun()
                }
            })

        }

        // //查询
        form.on('submit(searchBth)', function (data) {
            canteenIdSet = [];
            canteenIdSetList = [];

            let _data = data.field,
				param = _data;
			var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
			var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
			param.startTime = startTime;
			param.endTime = endTime;
            param.queryType = 2

			selectTable.reload({
				page:{curr:1},
				where: param
			})
        })

        table.on('toolbar(selectTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data; //获取选中行数据
            if(obj.event=='change'){ // 生成账单
                if (canteenIdSet.length == 0) {
                    layer.msg("请勾选需要生成账单的订单");
                    return;
                }
                http.myOpen({
                    title:'生成账单',
                    content: $('#tableForm'),
                    area:['900px','500px'],
                    btn:['确定','取消'],
                    success: function(){
                        canteenFun()
                    },
                    btn1:function(index, layero){
                        if(canteenIdSet.length == 0){
                            layer.msg("请勾选需要生成账单的订单");
                            return;
                        }
                        http.post('admin/order/bill/add/foodManager',{
                            shopOrderNoSet: canteenIdSet.toString(),
                            billType: 2
                        },function(res){
                            if(res.code == 200){
                                canteenIdSet = [] // 订单id集合
                                canteenIdSetList = [] // 选中订单
                                layer.msg('操作成功')
                                layer.close(index)
                                table.reload('selectTable');
                            }else{
                                layer.msg(res.msg)
                            }
                        })
                    }
                })
            }else if(obj.event=='export'){ // 导出
				var data = excelList; 
				if (data.length == 0) {
					layer.msg('当前页没有可导出的数据');
					return;
                }
                var param = {
                    page: currPage,
                    limit: 10,
                    authentication: localStorage.getItem('xi-can-management-authentication'),
                    startTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'',
                    endTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'',
                    orderStatusSet: $('input[name=orderStatusSet]').val(),
                    shopOrderNo: $('input[name=shopOrderNo]').val(),
                    foodManagerName: $('input[name=foodManagerName]').val(),
                    supplierName: $('input[name=supplierName]').val(),
                    canteenName: $('input[name=canteenName]').val(),
                    productName: $('input[name=productName]').val(),
                    foodManagerOrderBillStatus: $('select[name=foodManagerOrderBillStatus]').val(),
                    queryType: 2
                }
                if (param) {  
                    let paramsArray = [];  
                    //encodeURIComponent  
                    Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                    window.open(TUrl+"admin/shop/order/export?"+paramsArray.join('&'))
                }
			}
        })

    })

</script>
</html>